//
//  LBHomeTableViewController.swift
//  MicroBlog
//
//  Created by lj on 15/8/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

private let LSStatusNormalCellID = "LSStatusNormalCellID"

class LSHomeTableViewController: LSBaseTableViewController {
    
    /// 微博列表的数据模型
    private lazy var statusListViewModel = LSStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !LSUserAccountViewModel.sharedUserAccountViewModel.userLogon {
            
            visitorView?.setupInfo(nil, message: "刘亚伟又要去抽烟啦啦啦啦啦啦啦啦啦")
            
            return
        }
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserverForName(LSStatusPictureViewSelectedphotoNotification, object: nil, queue: nil) { [weak self](notification) -> Void in
            
            guard let urls = notification.userInfo![LSStatusPictureViewSelectedphotoURLsKey] as? [NSURL] else {
                return
            }
            
            guard let indexPath = notification.userInfo![LSStatusPictureViewSelectedphotoIndexPath] as? NSIndexPath else {
                return
            }
            
            
            let vc = LSPhotoBrowserViewController(urls: urls, indexPath: indexPath)
            
            //通知中只要用了self就会循环引用
            self?.presentViewController(vc, animated: true, completion: nil)
            
        }
        
        loadData()
        prepareForTableView()
    }
    
    
    //一定记得销毁通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private func prepareForTableView() {
        //注册cell
        tableView.registerClass(LSStatusCell.self, forCellReuseIdentifier: LSStatusNormalCellID)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        tableView.estimatedRowHeight = 350
        //        tableView.rowHeight = 400
        
        //        tableView.rowHeight = 200    }
    }
    
    /// 加载数据
    func loadData(){
        
        statusListViewModel.loadStatuses().subscribeError({ (error) -> Void in
            printLog(error)
            SVProgressHUD.showInfoWithStatus("网络不给力")
            
            }) { () -> Void in
                
                //刷新数据
                self.tableView.reloadData()
        }
    }
}


extension LSHomeTableViewController {
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.statuses.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(LSStatusNormalCellID, forIndexPath: indexPath) as! LSStatusCell
        
        //获取微博数据
        let statusViewModel = statusListViewModel.statuses[indexPath.row]
        
        cell.statusViewModel = statusViewModel
        
        return cell
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let viewModel = statusListViewModel.statuses[indexPath.row]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(LSStatusNormalCellID) as! LSStatusCell
        
        return cell.rowHeight(viewModel)
        
        
    }
}